﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SEO.Business;
using SEO.Entity;

public partial class EditArticleSEO : AdminPage
{
    /// <summary>
    /// ID
    /// </summary>
    protected int nID = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        int.TryParse(Common.GetQueryValue("id", 0), out nID);

        if (!Page.IsPostBack)
        {
            InitSort();
            //页面数据初始化
            InitData();
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.ArticleSEO;
    }

    protected void InitSort()
    {
        DataTable dtSort = new BisSort().GetList();
        ddlSort.Items.Clear();
        ddlSort.Items.Add(new ListItem("请选择分类", "0"));
        foreach (DataRow drSort in dtSort.Rows)
        {
            ddlSort.Items.Add(new ListItem(drSort["Name"].ToString(), drSort["ID"].ToString()));
        }
        dtSort.Dispose();
    }

    /// <summary>
    /// 数据初始化
    /// </summary>
    protected void InitData()
    {
        ArticleSEO Info = new BisArticleSEO().Get(nID, nAdminUserID);
        if (Info != null)
        {
            txtUrl.Text = Info.ArticleUrl.Trim();
            ddlSort.SelectedValue = Info.SortID.ToString();
        }
    }

    /// <summary>
    /// 提交操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string strArticleUrl = txtUrl.Text.Trim().ToLower();
        int nSortID = Convert.ToInt32(ddlSort.SelectedValue);
        BisArticleSEO bisInfo = new BisArticleSEO();
        if (bisInfo.CheckArticlesUrl(nID, strArticleUrl))
        {
            Until.Alert("文章网址已经存在！");
            return;
        }
        else
        {
            bool bResult = bisInfo.UpdateUrl(nID,nSortID,nAdminUserID, strArticleUrl);
            //结果处理
            if (bResult)
            {
                Until.AlertJS("提交成功！", "ResetPage();");
            }
            else
            {
                Until.Alert("提交失败！");
            }
        }
    }
}